package com.lcj.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;

import groovyjarjarantlr.debug.InputBufferReporter;

public class HelloWord {

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义
	 */
	@Test
	public void developer() {
		Deployment deploy = processEngine.getRepositoryService() // 与流程定义和部署对象相关的service
				.createDeployment() // 创建一个部署对象
				.name("helloworld入门程序") //添加部署的名称
				.addClasspathResource("diagrams/MyProcess.bpmn")// 从classpath中加载 文件 一次只能加载一个
				.addClasspathResource("diagrams/MyProcess.png").deploy(); // 完成部署
		System.out.println(deploy.getId());
		System.out.println(deploy.getName());
		System.out.println(deploy.getCategory());
	}
	
	/**
	 * 启动流程实例
	 */
	@Test
	public void runProcess() {
		ProcessInstance processInstance = processEngine.getRuntimeService() //与正在执行的流程实例和执行对象相关的对象
						.startProcessInstanceByKey("helloworld"); //使用流程定义的key启动流程实例  可以按照最新版本启动
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getProcessDefinitionId());
		System.out.println(processInstance.getActivityId());
		System.out.println(processInstance.getName());
	}
	
	/**
	 * 查询当前人的个人任务
	 */
	@Test
	public void task() {
		List<Task> list = processEngine.getTaskService()//与正在执行对象任务管理相关的service
						.createTaskQuery() //获取查询对象
						.taskAssignee("ww").list(); //指定办理人
		if(list!=null && list.size()>0) {
			for(Task t : list) {
				System.out.println(t.getId());
				System.out.println(t.getName());
				System.out.println(t.getAssignee()); //办理人
				System.out.println(t.getCreateTime());
				System.out.println(t.getProcessDefinitionId());
				System.out.println(t.getExecutionId());
			}
		}
	}
	
	/**
	 * 完成当前的任务
	 */
	@Test
	public void complate() {
		String taskid = "10002";
		processEngine.getTaskService().complete(taskid);
		System.out.println("任务完成 "+taskid);
	}
	
	
	/**
	 * 从zip文件中创建流程定义
	 */
	@Test
	public void createProcessByZip() {
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/MyProcess.zip");
		ZipInputStream zip = new ZipInputStream(in);
		Deployment deploy = processEngine.getRepositoryService()
						.createDeployment()
						.name("从zip中部署流程")
						.addZipInputStream(zip).deploy();
		System.out.println(deploy.getId());
		System.out.println(deploy.getName());
		System.out.println(deploy.getDeploymentTime());
		System.out.println(deploy.getTenantId());
	}
	
	/**
	 * 查询流程定义
	 */
	@Test
	public void findProcess() {
		List<ProcessDefinition> list = processEngine.getRepositoryService()
						.createProcessDefinitionQuery()
						.orderByProcessDefinitionVersion().desc().list();
		if(list!=null&& list.size()>0) {
			for(ProcessDefinition p : list) {
				System.out.println(p.getId());
				System.out.println(p.getCategory());
				System.out.println(p.getName());
				System.out.println(p.getKey());
				System.out.println(p.getVersion());
				System.out.println(p.getDeploymentId());
				System.out.println(p.getResourceName());
				System.out.println(p.getDiagramResourceName());
				System.out.println("###########################");
			}
		}
	}
	
	
	/**
	 * 删除流程定义
	 */
	@Test
	public void deleteProcess() {
		String deploymentId = "2501";
		processEngine.getRepositoryService()
						.deleteDeployment(deploymentId, true); //后面参数为true代表级联删除
		System.out.println("删除成功！");
	}
	
	
	/**
	 * 查看流程图
	 * @throws Exception 
	 * @throws FileNotFoundException 
	 */
	@Test
	public void showProcessPic() throws FileNotFoundException, Exception {
		String deploymentId = "15001";
		List<String> names = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
		
		String resourceName = "";
		if(names!=null && names.size()>0) {
			for(String name : names) {
				if(name.contains(".png")) {
					resourceName = name ;
				}
			}
		}
		
		
		InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, resourceName);
		File file = new File("D:/"+resourceName);
		FileCopyUtils.copy(inputStream,new FileOutputStream(file));
		System.out.println("文件获取完成");
	}
}
